package com.wangxiang.shop.order.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wangxiang.common.utils.*;
import com.wangxiang.shop.order.dao.MemberOrderDao;
import com.wangxiang.shop.order.entity.MemberOrderEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.wangxiang.shop.order.dao.ShipOrderDao;
import com.wangxiang.shop.order.entity.ShipOrderEntity;
import com.wangxiang.shop.order.service.ShipOrderService;


@Service("shipOrderService")
public class ShipOrderServiceImpl extends ServiceImpl<ShipOrderDao, ShipOrderEntity> implements ShipOrderService {
    @Autowired
    private ShipOrderDao shipOrderDao;
    @Autowired
    private MemberOrderDao memberOrderDao;


    @Override
    public int add(HashMap<String, Integer> map) {
        ShipOrderEntity shipOrderEntity = new ShipOrderEntity();
        int orderId = map.get("orderId");
        int userId = map.get("userId");
        shipOrderEntity.setOrderId(orderId);
        shipOrderEntity.setShipUserId(userId);
        shipOrderEntity.setTime(new Date());
        int insert = shipOrderDao.insert(shipOrderEntity);
        if (insert>0){
            //更新全局表状态
            int i = memberOrderDao.updateStatus(orderId, 1);//已发货
            return i;
        }else {
            return 0;
        }

    }

    @Override
    public PageEntity page(HashMap<String, Integer> map) {
        PageHelper.startPage(map.get("pageNum"),map.get("pageSize"));
        List<ShipOrderEntity> entitys = shipOrderDao.selectList(null);
        entitys.forEach((item)->{
            TimeUtils timeUtils = new TimeUtils();
            String s = timeUtils.timeChange(item.getTime());
            item.setStrTime(s);
        });
        PageInfo<ShipOrderEntity> shipOrderEntityPageInfo = new PageInfo<>(entitys);
        PageGenerate pageGenerate = new PageGenerate();
        PageEntity generate = pageGenerate.generate(shipOrderEntityPageInfo);


        return generate;
    }

    @Override
    public List<HashMap<String, Object>> pageList(List<ShipOrderEntity> list) {

        ArrayList<HashMap<String, Object>> hashMaps = new ArrayList<>();
        for (ShipOrderEntity shipOrderEntity : list) {
            HashMap<String, Object> map = new HashMap<>();
            //查订单
            MemberOrderEntity entity = memberOrderDao.selectById(shipOrderEntity.getOrderId());

            //组合
            map.put("orderId",entity.getOrderId());
            map.put("userId",entity.getUserId()); //这里的购买人
            map.put("doUserId",shipOrderEntity.getShipUserId());  //操作发货的管理员
            map.put("shipTime" , shipOrderEntity.getStrTime());  //发货时间

            TimeUtils timeUtils = new TimeUtils();
            String s = timeUtils.timeChange(entity.getTime());
            map.put("orderTime",s); //下单时间
            map.put("spuId",entity.getSpuId());
            map.put("num",entity.getNum());
            map.put("price",entity.getPrice());
            map.put("skuId",entity.getSkuId());
            map.put("receiptId",entity.getReceiptId());
            hashMaps.add(map);



        }
        return hashMaps;
    }
}